package db

import (
	"fmt"
	"github.com/simplechain-org/client/log"
	core "xorm.io/xorm/log"
)

type logger struct {
	showSQL bool
	level   core.LogLevel
}

const prefix = " "

func (l *logger) Debug(v ...interface{}) {
	if l.level <= core.LOG_DEBUG {
		log.Debug(prefix, v...)
	}
}
func (l *logger) Debugf(format string, v ...interface{}) {
	if l.level <= core.LOG_DEBUG {
		log.Debug(fmt.Sprintf(prefix+format, v...))
	}
}
func (l *logger) Error(v ...interface{}) {
	if l.level <= core.LOG_ERR {
		log.Error(prefix, v...)
	}
}
func (l *logger) Errorf(format string, v ...interface{}) {
	if l.level <= core.LOG_ERR {
		log.Error(fmt.Sprintf(prefix+format, v...))
	}
}
func (l *logger) Info(v ...interface{}) {
	if l.level <= core.LOG_INFO {
		log.Info(prefix, v...)
	}
}
func (l *logger) Infof(format string, v ...interface{}) {
	if l.level <= core.LOG_INFO {
		log.Info(fmt.Sprintf(prefix+format, v...))
	}
}
func (l *logger) Warn(v ...interface{}) {
	if l.level <= core.LOG_WARNING {
		log.Warn(prefix, v...)
	}
}
func (l *logger) Warnf(format string, v ...interface{}) {
	if l.level <= core.LOG_WARNING {
		log.Warn(fmt.Sprintf(prefix+format, v...))
	}
}

func (l *logger) Level() core.LogLevel {
	return l.level
}
func (l *logger) SetLevel(level core.LogLevel) {
	l.level = level
}

func (l *logger) ShowSQL(show ...bool) {
	if len(show) == 0 {
		l.showSQL = true
		return
	}
	l.showSQL = show[0]
}
func (l *logger) IsShowSQL() bool {
	return l.showSQL
}
